package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private IProductUnitService productUnitService;

    @Operation(summary = "商品单位分页查询")
    @GetMapping("list")
    @RequiresPermissions("base:product:Unit:list")
    public TableDataInfo list(@RequestParam(value = "name",required = false) String name){
        //开始分页
        startPage();
        //根据业务层条件查询商品单位列表
        List<ProductUnit> list = productUnitService.selectProductUnitList(name);
        //将查询结果封装到分页对象中
        TableDataInfo dataTable = getDataTable(list);
        //返回查询结果
        return dataTable;
    }

    @Operation(summary = "获取商品单位的详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        ProductUnit byId = productUnitService.getById(id);
        AjaxResult success = AjaxResult.success(byId);
        return success;
    }

    @Operation(summary = "新增商品单位")
    @PostMapping
    public AjaxResult add(@RequestBody ProductUnit productUnit){
        productUnitService.save(productUnit);
        return AjaxResult.success();
    }

    @Operation(summary = "删除商品单位")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") List<Long> ids){
        productUnitService.removeByIds(ids);
        return AjaxResult.success();
    }

    @Operation(summary = "更新商品单位")
    @PutMapping
    public AjaxResult update(@RequestBody ProductUnit productUnit){
        productUnitService.update(productUnit,null);
        return AjaxResult.success();
    }

    @Operation(summary = "查询所有的商品单位")
    @GetMapping("getUnitAll")
    public AjaxResult getUnitAll(){
        List<ProductUnit> list = productUnitService.list();
        return AjaxResult.success(list);
    }
}
